import java.util.HashSet;

public class L202 {

    public static void main(String[] args) {

        boolean happy = new Solution().isHappy(19);
        System.out.println(happy);


    }

    static class Solution {
        public boolean isHappy(int n) {
            HashSet<Integer> sums = new HashSet<>();
            int sum = 0;

            while (sum != 1) {
                sum = 0;
                while (n > 0) {
                    int temp = n % 10;
                    sum += temp * temp;
                    n = n / 10;
                }
                if (!sums.add(sum)) {
                    break;
                }
                n = sum;

            }
            return sum == 1;
        }
    }
}
